package main

import (
	"log"
	"os"
	"os/signal"
	"report/core"
	"report/global"
	"report/initialize"
	"syscall"
)

func main() {
	global.GVA_VP = core.Viper()
	global.DB = initialize.Gorm()
	if global.DB != nil {
		initialize.MysqlTables(global.DB) //初始化表
		db, _ := global.DB.DB()
		defer db.Close()
	} else {
		panic("数据库连接失败")
	}

	initialize.RegisterNacos()
	core.InitGRPCServer()
	defer core.ShutdownGRPCServer()

	// 等待中断信号以优雅关闭服务
	waitForShutdownSignal()
}

func waitForShutdownSignal() {
	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
	<-sigCh

	// 收到中断信号，开始关闭服务
	log.Println("Received shutdown signal. Shutting down...")

	// 关闭 gRPC 服务器
	log.Println("Shutting down gRPC server")
	core.ShutdownGRPCServer()
}
